package client2;


import java.net.*;
import java.util.ArrayList;
import java.util.Random;

class TestGen2
{
	public static ArrayList<Integer> portnos = new ArrayList<Integer>();
	public static void main(String args[]) throws Exception
	{
		if(args.length==3){
			DatagramSocket clientSocket = new DatagramSocket();
			fillArray();
			InetAddress IPAddress = InetAddress.getByName(args[0]);
			byte[] sendData = new byte[1024];
			String sentence;
			int port = Integer.parseInt(args[1]);
			int noPackets=Integer.parseInt(args[2]);
			while(noPackets!=0){
				sentence = getNewPacket();
				if(sentence.equalsIgnoreCase("exit")){
					break;
				}
				//System.out.println(sentence);
				sendData = sentence.getBytes();
				DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
				clientSocket.send(sendPacket);
				noPackets--;
				try{
					Thread.currentThread();
					Thread.sleep(4000);//sleep for 4000 ms
				}
				catch(Exception ie){
				}
			}
			clientSocket.close();
		}
		else{
			System.out.println("you need  to give 3 args");
		}
	}

	private static void fillArray() {
		// TODO Auto-generated method stub

		portnos.add(21);
		portnos.add(22);
		portnos.add(23);
		portnos.add(25);
		portnos.add(42);
		portnos.add(70);
		portnos.add(80);
		portnos.add(88);
		portnos.add(109);
	}

	private static String getNewPacket() {
		String data=null;
		data = getIP() +" "+ getIP() +" "+ getPort()+ " "+getPort()+" "+getFlag();
		return  data;
	}

	private static String getFlag() {
		RandomNo r = new RandomNo();
		int i = r.nextInt(1, 3);
		switch(i){
		case 1 :return "start";
		case 2 :return "continue";
		case 3 :return "end";
		default: return "start";		
		}
	}

	private static String getPort() {
		RandomNo r = new RandomNo();
		Integer temp[] =(Integer[])portnos.toArray(new Integer[0]);
		Integer i = r.nextInt(0, temp.length-1);
		return temp[i].toString();
	}

	private static String getIP() {
		RandomNo r = new RandomNo();
		Integer i = 192;
		String s1 = i.toString();
		for(int k=0;k<3;k++){
			i = r.nextInt(0, 255);
			s1+=".";
			s1 += i.toString();
		}
		//System.out.println(s1);
		return s1;
	}
}

final class RandomNo extends Random{
	public int nextInt(int lower,int upper){
		return nextInt((upper-lower+1))+lower;

	}
}
